﻿using System;
using System.Threading;
using ExtendLib.Helper;
using InterfaceLib;
using Ivony.Fluent;

namespace ExtendLib
{
    /// <summary>
    /// 适用于基本发送配置
    /// </summary>
    public class ExtendOne : IMsgHandler
    {
        /// <summary>
        /// 代理Ip
        /// </summary>
        private string ProxyIp { get; set; }
        /// <summary>
        /// 代理端口
        /// </summary>
        private int ProxyPort { get; set; }

        public MsgModel InfoModel { get; set; }
        public string PhoneNumber { get; set; }
        public ExtendOne(MsgModel model, string phone, string ip ="", string port ="")
        {
            ProxyIp = ip;
            ProxyPort = int.Parse(port.IsNullOrEmpty() ? "0" : port);

            InfoModel = model;
            InfoModel.Wait = InfoModel.Wait * 1000;
            PhoneNumber = phone;
        }
        public bool Verify(out string errMsg)
        {
            HttpRequestItem hri = new HttpRequestItem
            {
                Url = InfoModel.BaseUrl,
                UserAgent = InfoModel.UseAgent,
                Referer = InfoModel.BaseUrl,
                ProxyPort = this.ProxyPort,
                ProxyIp = this.ProxyIp
            };
            var rd = HttpHelper.GetHtml(hri);
            if (InfoModel.RequestType == ERequestType.Post)
            {
                hri.Url = InfoModel.BaseUrl + InfoModel.PostUrl;
                hri.PostData = string.Format(InfoModel.PostData, PhoneNumber);

                hri.Method = "post";
                hri.ContentType = "post";
            }
            else
            {
                hri.Url = InfoModel.BaseUrl + string.Format(InfoModel.PostUrl, PhoneNumber);
            }
            hri.IsAjax = true;
            hri.Cookie = rd.Cookie.IsNullOrEmpty() ? "" : rd.Cookie.Replace(" ", "").Replace("path=/", "").Replace("Path=/", "").Replace("Path=\"\"", "");
           // hri.Referer = InfoModel.BaseUrl + InfoModel.PostUrl;
            rd = HttpHelper.GetHtml(hri);
            var content = rd.DataInfo;
            errMsg = content;
            if (content.IsNullOrEmpty())
                return false;
            if (content.IndexOf(InfoModel.SuccessFlag, StringComparison.InvariantCultureIgnoreCase) > 0)
            {
               // errMsg = "";
                return true;
            }
            return false;
        }

        public void SendMsg()
        {
            while (true)
            {
                if(ControlHelper.GetStatus(EOtherType.发送状态)==false) //停止标记
                    break;
                Thread.Sleep(InfoModel.Wait);
                HttpRequestItem hri = new HttpRequestItem
                {
                    Url = InfoModel.BaseUrl,
                    UserAgent = InfoModel.UseAgent,
                    Referer = InfoModel.BaseUrl,
                    ProxyPort = this.ProxyPort,
                    ProxyIp = this.ProxyIp
                };
                var rd = HttpHelper.GetHtml(hri);
                if (InfoModel.RequestType == ERequestType.Post)
                {
                    hri.Url = InfoModel.BaseUrl + InfoModel.PostUrl;
                    hri.PostData = string.Format(InfoModel.PostData, PhoneNumber);
                    hri.Method = "post";
                    hri.ContentType = "post";
                }
                else
                {
                    hri.Url = InfoModel.BaseUrl + string.Format(InfoModel.PostUrl, PhoneNumber);
                }
                hri.IsAjax = true;
               // hri.Referer = InfoModel.BaseUrl + InfoModel.PostUrl;
                hri.Cookie = rd.Cookie.IsNullOrEmpty() ? "" : rd.Cookie.Replace(" ", "").Replace("path=/", "").Replace("Path=/", "").Replace("Path=\"\"", "");
                rd = HttpHelper.GetHtml(hri);
                var content = rd.DataInfo;
                if (content.IsNullOrEmpty())
                {
                    LogHelper.WirteLog(string.Format("请求{0}失败，返回为空",InfoModel.BaseUrl+InfoModel.PostUrl));
                    break;
                }
                if (content.IndexOf(InfoModel.SuccessFlag, StringComparison.InvariantCultureIgnoreCase) > 0)
                {
                    LogHelper.WirteLog(string.Format("请求{0}成功，返回内容为：{1}", InfoModel.BaseUrl + InfoModel.PostUrl, content));
                    continue;
                }
                LogHelper.WirteLog(string.Format("请求被限制，返回内容为：{0}", content));
                //一直发送 直到网站报错
                break;
            }
        }
    }
}
